//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormMain.h"
#include "FormItems.h"
#include "FormItemCategories.h"
#include "FormItemNames.h"
#include "FormItemBasicUnits.h"
#include "FormItemDrugMedForms.h"
#include "FormItemDrugPharmGroups.h"
#include "FormItemDrugGenericNames.h"
#include "FormItems.h"
#include "FormSendTesterReport.h"
#include "FormTesterReportsViewer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBGridEh"
#pragma link "DBGridEhGrouping"
#pragma link "GridsEh"
#pragma link "FIBDatabase"
#pragma link "FIBDataSet"
#pragma link "pFIBDatabase"
#pragma link "pFIBDataSet"
#pragma link "pFIBErrorHandler"
#pragma link "PropStorageEh"
#pragma link "PropFilerEh"
#pragma link "SIBEABase"
#pragma link "SIBFIBEA"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
	PropStorageEh->Section = this->Name;
	Application->HintHidePause = 60000;
	LastActiveForm = this;
//	TStyleManager* StyleManager = new TStyleManager();
//	StyleManager->TrySetStyle("Carbon");
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormShow(TObject *Sender)
{
	RemoteDatabase->Open(true);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormHide(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FIBErrorHandlerFIBErrorEvent(TObject *Sender, EFIBError *ErrorValue, TKindIBError KindIBError, bool &DoRaise)
{
	switch(KindIBError) {
//		case keUniqueViolation:
//			throw Exception("Value already exists");
//			break;
//		case keCheck:
//			throw Exception("Wrong value");
//			break;
		case keForeignKey:
			throw Exception("Can't delete used values");
			break;
		case keException:
			if(FIBErrorHandler->ExceptionName == "MEDICINE_CATEGORY_CHANGE_DENIED")
				throw Exception("You can't change Category from or to \"Medicine\" for already used Item Names");
			else if(FIBErrorHandler->ExceptionName == "HARDCODED_RECORD")
				throw Exception("You trying to change or delete data that's used in program and can't be touched");
			break;
//		case keOther:
//			switch(ErrorValue->IBErrorCode) {
//				case isc_not_valid:
//					throw Exception("Empty values not allowed");
//					break;
//			}
//			break;
	}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ItemsMenuClick(TObject *Sender)
{
	ItemsForm->Show();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ItemCategoriesMenuClick(TObject *Sender)
{
	ItemCategoriesForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ItemKindsMenuClick(TObject *Sender)
{
	ItemNamesForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ItemBasicUnitsMenuClick(TObject *Sender)
{
	ItemBasicUnitsForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::DrugDosageFormsMenuClick(TObject *Sender)
{
	ItemDrugMedFormsForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::PharmacepticalGroupsMenuClick(TObject *Sender)
{
	ItemDrugPharmGroupsForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::DrugsGenericNamesMenuClick(TObject *Sender)
{
	ItemDrugGenericNamesForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ItemsToolButtonClick(TObject *Sender)
{
	ItemsForm->Show();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch(Key) {
		case VK_F2:
			ItemsForm->Show();
			break;
	}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ApplicationEventsMessage(tagMSG &Msg, bool &Handled)
{
	if(Msg.message == WM_RBUTTONDOWN && GetKeyState(VK_CONTROL)&0x8000 && GetKeyState(VK_MENU)&0x8000) {
// Send Tester Report Procedure
		TPoint MousePos = Mouse->CursorPos;
		TControl* Control = FindControl(Msg.hwnd);
		TControl* SubControl = NULL;
		while(Control) {
			TMetaClass* meta = Control->ClassParent();
			while(meta && meta->ClassName() != "TWinControl") meta = meta->ClassParent();
			if(!meta) {
				SubControl = NULL;
				break;
			}
			SubControl = ((TWinControl*)Control)->ControlAtPos(Control->ScreenToClient(MousePos), true, true, false);
			if(!SubControl || SubControl->Name.IsEmpty()) break;
			Control = SubControl;
		}

		String ControlName = Screen->FocusedForm->Name;
		if(Control) ControlName = ControlName  + "->" + Control->Name;
		if(SubControl) ControlName = ControlName + "->" + SubControl->ClassName();

		SendTesterReportForm->ReportedControlLabel->Caption = ControlName;
		SendTesterReportForm->ShowModal();
		Handled = true;
	}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ReportsViewerMenuClick(TObject *Sender)
{
	TesterReportsViewerForm->Show();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::SendCommonReportClick(TObject *Sender)
{
	SendTesterReportForm->ReportedControlLabel->Caption = "- Common -";
	SendTesterReportForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ExitMenuClick(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::ChangeUserMenuClick(TObject *Sender)
{
	RemoteDatabase->Close();
	RemoteDatabase->UseLoginPrompt = true;
//	RemoteDatabase->ConnectParams->UserName = "";
//	RemoteDatabase->ConnectParams->RoleName = "";
//	RemoteDatabase->ConnectParams->Password = "";
	RemoteDatabase->Open();
	RemoteDatabase->UseLoginPrompt = false;
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::RemoteDatabaseAfterConnect(TObject *Sender)
{
	Caption = "Owlogist [ " + RemoteDatabase->ConnectParams->UserName + " / " + RemoteDatabase->ConnectParams->RoleName + " ]";
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ApplicationEventsDeactivate(TObject *Sender)
{
	LastActiveForm = Screen->ActiveForm;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ApplicationEventsActivate(TObject *Sender)
{
	LastActiveForm->SetFocus();
}
//---------------------------------------------------------------------------

